package cn.agiledata.bank.linkbank.impl.ceb;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayResponse;
import cn.agiledata.bank.linkbank.logic.DigitalSignValidatable;

/**
 * 光大银行网上银行b2c支付响应
 * 
 * @author 马贤波
 * 
 */
public final class CebB2cNetPayResponse extends AbstractBankPayResponse
        implements DigitalSignValidatable {
    private static final String[] RESPONSE_KEYS = {CebNetPayConstant.PLAIN,
		CebNetPayConstant.SIGNATURE};


    protected AbstractBankPayResponse getCopiedInstance() {
        CebB2cNetPayResponse response = new CebB2cNetPayResponse();
        return response;
    }

    protected String getSuccessReturnCode() {
        return CebNetPayConstant.SUCCESS;
    }
    
    public Date getDate() {
        String dateValue = getMessage(CebNetPayConstant.DATE);
        return DateUtil.string2Date(dateValue, CebNetPayConstant.DATE_FORMAT);
    }

    protected String[] getValidKeys() {
        return RESPONSE_KEYS;
    }

    protected boolean isValidKey(String key) {
        return super.isValidKey(key);
    }

    public BigDecimal getAmount() {
        try {
            BigDecimal amount = new BigDecimal(
                    getMessage(CebNetPayConstant.AMOUNT));
            return amount.setScale(2);
        } catch (Exception e) {
            return null;
        }
    }

    public String getOrderId() {
        return getMessage(CebNetPayConstant.ORDER_ID);
    }

    public String getReturnCode() {
    	// 根据与光大银行沟通，其只要返回报文即认为交易成功，所以此处固定为成功交易
    	// zln 2008-05-13
    	return CebNetPayConstant.SUCCESS;
//        return getMessage(CebB2cNetConstant.RETURN_CODE);
    }

    public String getReturnMessage() {
        return getMessage(CebNetPayConstant.REMARK);
    }

    public String getDigitalSign() {
        return getMessage(CebNetPayConstant.SIGNATURE);
    }
    
    public String getPlain() {
        return getMessage(CebNetPayConstant.PLAIN);
    }

    public String getCryptoKeyDescription() {
        return null;
    }

    public boolean validateCryptoKey(String cryptoKey) {
        return true;
    }

    public boolean validateDigitalSign(String cryptoKey)
            throws LinkBankException {
    	// 校验返回签名
    	boolean bolReturn = CebNetPayUtils.validateDigitalSign(getDigitalSign(),getPlain());
    	
    	// 由于光大银行的数据内容均在Plain中使用拼接的字符串返回，所以在校验成功后，解析明文
    	if(bolReturn) {
    		Map mapResponse = CebNetPayUtils.string2map(getPlain());
    		// 将解析后的内容放到message
    		for (Iterator iter = mapResponse.keySet().iterator(); iter.hasNext();) {
				String strKey = (String) iter.next();
				setMessage(strKey, (String)mapResponse.get(strKey));
			}
    	}
    	
        return bolReturn;
    }

}
